package com.siwei.mes.common.util;

import lombok.extern.slf4j.Slf4j;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;

/**
 * @author chenmaolin
 * @version 1.0
 * @date 2023/7/15
 * @Description 距离工具类
 */
@Slf4j
public class DistanceUtils {
    /**
     * 默认地球半径
     */
    private static double EARTH_RADIUS = 6371000;//赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * @param lon1 第一点的经度
     * @param lat1 第一点的纬度
     * @param lon2 第二点的经度度
     * @param lat2 第二点的纬度
     * @return 返回的距离，单位m
     */
    public static double getDistance(double lon1, double lat1, double lon2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        log.info("比较两个经纬度的距离为：[{}],lon1:{},lat1:{},lon2:{},lat2:{}", s, lon1, lat1, lon2, lat2);
        return s;
    }

    /**
     * @param longitudeFrom 第一点的经度
     * @param latitudeFrom  第一点的纬度
     * @param longitudeTo   第二点的经度度
     * @param latitudeTo    第二点的纬度
     * @return 返回的距离，单位m
     */
    public static double getEllipsoidalDistance(double longitudeFrom, double latitudeFrom, double longitudeTo, double latitudeTo) {
        GlobalCoordinates source = new GlobalCoordinates(latitudeFrom, longitudeFrom);
        GlobalCoordinates target = new GlobalCoordinates(latitudeTo, longitudeTo);
        double distance = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, source, target).getEllipsoidalDistance();
        distance = BigDecimal.valueOf(distance).setScale(4, BigDecimal.ROUND_HALF_DOWN).doubleValue();
        log.info("比较两个经纬度的距离为：[{}],lon1:{},lat1:{},lon2:{},lat2:{}", distance, longitudeFrom, latitudeFrom, longitudeTo, latitudeTo);
        return distance;
    }

    /**
     * 计算中心经纬度与目标经纬度的距离（米）
     *
     * @param centerLon 中心经度
     * @param centerLat 中心纬度
     * @param targetLon 需要计算的经度
     * @param targetLat 需要计算的纬度
     * @return 米
     */
    private static double distance(double centerLon, double centerLat, double targetLon, double targetLat) {
        double lon = 102834.74258026089786013677476285;// 每经度单位米;
        double lat = 111712.69150641055729984301412873;// 每纬度单位米;
        double b = Math.abs((centerLat - targetLat) * lon);
        double a = Math.abs((centerLon - targetLon) * lat);
        double sqrt = Math.sqrt((a * a + b * b));
        log.info("比较两个经纬度的距离为：[{}],lon1:{},lat1:{},lon2:{},lat2:{}", sqrt, centerLon, centerLat, targetLon, targetLat);
        return sqrt;
    }


    public static void main(String[] args) {
        double lon = 121.5148660; //当前经度
        double lat = 31.3145090; //当前纬度
        double lon1 = 121.5042370;
        double lat1 = 31.3108910;
        double dist = getDistance(lon, lat, lon1, lat1);
        double dist2 = distance(lon, lat, lon1, lat1);
        double dist3 = getEllipsoidalDistance(lon, lat, lon1, lat1);
        System.out.println("1.两点相距：" + dist + " 米");
        System.out.println("2.两点相距：" + dist2 + " 米");
        System.out.println("3.两点相距：" + dist3 + " 米");
    }
}
